import {Component, OnInit, ViewEncapsulation} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {EventsService} from "@core/events/events.service";
import {GoodsService} from "@goods/goods.service";
import {Subject} from "rxjs/Rx";
import {ToolsService} from "@shared/service/tools/tools.service";
import {AuthService} from "@shared/service/auth/auth.service";
import {GoodsTicketService} from "@goods/goods-ticket/goods-ticket.service";

@Component({
  selector: 'seed-goods-ticket-detail',
  templateUrl: './goods-ticket-detail.component.html',
  styleUrls: ['./goods-ticket-detail.component.scss'],
  encapsulation: ViewEncapsulation.None,
})

export class GoodsTicketDetailComponent implements OnInit {
  public params = {
    page: 1,
    rows: 10,
  };
  public goodsCode: string;
  public ticketDetail;
  private ticketStatus = false;
  public goodsStatusText = '立即购买';
  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private goodsTicketService: GoodsTicketService,
    private tools: ToolsService,
    private events: EventsService,
    private authService: AuthService,
  ) {
    this.route.params.subscribe(params => {
      this.goodsCode = params['goodsCode'];
    });
     this.getGoodsTicketDetail();
  }

  /**
   * 获取票务服务列表
   */
  getGoodsTicketDetail() {
    this.goodsTicketService.getGoodsTicketDetail(this.goodsCode).subscribe(res => {
      if (res.result === '0' && res.data) {
        this.ticketDetail = res.data;
        console.log(this.ticketDetail.code);
        console.log(this.ticketDetail.favorited);
        this.handleGoodsStatus(this.ticketDetail);
      }
    });
  }

  // 跳转登录页面
  goToLogin() {
    this.router.navigate(['/auth/login']);
  }

  /**
   * @description 点击进入商品FAQ详情页
   * @author @zhoukf1349
   * @date 2018-03-29
   * @param goodsCateCode
   */
  goToGoodsFAQ(code: string) {
    this.router.navigateByUrl(`/goods/p/faq/${code}`);
  }

  /**
   * @description 处理商品收藏
   * @author @zhoukf1349
   * @date 2018-04-04
   */
  handleCollect() {
    const data = {
      bizId: this.goodsCode,
      favoriteType: 3
    };
    if (this.ticketDetail.favorited === false) {
      this.goodsTicketService.addFavorites(data).subscribe((res) => {
        if (res.result === '0') {
          this.ticketDetail.favorited = true;
          this.ticketDetail.favoritedCount = this.ticketDetail.favoritedCount + 1;
          this.tools.showToast("收藏成功");
        }
      });
    } else {
      this.goodsTicketService.deleteFavorites(data).subscribe((res) => {
        if (res.result === '0') {
           this.ticketDetail.favorited = false;
          this.ticketDetail.favoritedCount = this.ticketDetail.favoritedCount - 1;
          this.tools.showToast("取消收藏");
        }
      });
    }
  }
  /**
   * @description 处理商品是否能购买状态
   * @author @zhoukf1349
   * @date 2018-04-04
   * @param val
   */
  set disposeGoodsPurchase(val: boolean) {
    this.ticketStatus = val;
  }

  get disposeGoodsPurchase(): boolean {
    return this.ticketStatus;
  }
  /**
   * @description 点击购买按钮事件,已下架，已售罄不做操作
   * @author @zhoukf1349
   * @date 201804-04
   * @param modal
   */

  handlePurchaseEvent(modal) {
    if (this.disposeGoodsPurchase) {
      return false;
    }
    if (this.isLogin()) {
      if (modal) {
        modal.show();
      }
    } else {
      this.authService.selectGoToLogin();
    }
  }

  isLogin(): boolean {
    return this.authService.isLoggedIn;
  }
  /**
   * @description 判断商品状态是否能购买
   * @author @zhoukf1349
   * @date 2018-04-04
   * @param data
   * @returns {boolean}
   */
  handleGoodsStatus(data) {
    if (data.status === 1) {
      this.goodsStatusText = '已下架';
      this.disposeGoodsPurchase = true;
      return false;
    }
    if (data.status === 2) {
      this.goodsStatusText = '立即购买';
      this.disposeGoodsPurchase = false;
      return false;
    }
    if (data.status === 3) {
      this.goodsStatusText = '预热中';
      this.disposeGoodsPurchase = false;
      return false;
    }
    if (data.status === 4) {
      this.goodsStatusText = '已售罄';
      this.disposeGoodsPurchase = true;
      return false;
    }
  }

  // 关闭模态框
  closeModal() {
    this.events.publish('modal:close');
  }

  ngOnInit() {
  }

}
